package cn.ccit.DAOImp;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.DishesDAO;
import cn.ccit.entites.DiningTable;
import cn.ccit.entites.Dishes;
import cn.ccit.util.MyDBUtil;

public class DishesDAOimp implements DishesDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(Dishes.class);
	@Override
	public boolean addDishes(Dishes dish) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(dish);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("add dishes fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false; 
	}

	@Override
	public boolean removeDishes(Dishes dish) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(dish);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove dishes fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean changeDishesInfo(Dishes dish) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(dish);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("change dishesInfo fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public List<Dishes> getSomeDishes(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from Dishes as dishes order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from Dishes").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeDishes fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public Dishes searchDishesByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from Dishes as dishes where dishes.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				Dishes dishes=(Dishes)query.list().get(0);
				session.beginTransaction().commit();
				return dishes;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search dishesInfo by id",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public Dishes searchDishesByName(String name) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from Dishes as dishes where dishes.name=?";
				Query query=session.createQuery(hql);
				query.setString(0, name);
				Dishes dishes=(Dishes)query.list().get(0);
				session.beginTransaction().commit();
				return dishes;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search dishesInfo by name fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<Dishes> searchDishesByShortFor(String name) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				
				session.beginTransaction();
				String hql="from Dishes as dishes where  dishes.shortFor like ? or dishes.name like ? or dishes.enumName like ?";
				Query query=session.createQuery(hql);
				query.setString(0, "%"+name+"%");
				query.setString(1, "%"+name+"%");
				query.setString(2, "%"+name+"%");
				List l=query.list();
				if(l.isEmpty()){
					return null;
				}
				Dishes dishes=(Dishes)l.get(0);
				session.beginTransaction().commit();
				return l;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search dishes by shortfor fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	
	@Override
	public List<Dishes> searchDishesByKind(String enumName) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				
				session.beginTransaction();
				String hql="from Dishes as dishes where  dishes.enumName = ?";
				Query query=session.createQuery(hql);
				query.setString(0, enumName);
				List l=query.list();
				if(l.isEmpty()){
					return null;
				}
				Dishes dishes=(Dishes)l.get(0);
				session.beginTransaction().commit();
				return l;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search dishes by shortfor fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	
}
